package com.danielschon.triangularity.activities;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.danielschon.triangularity.sprites.Ship;
import com.danielschon.triangularity.utility.Util;
import com.danielschon.triangularity.utility.Vec;

import android.opengl.GLES20;
import android.util.Log;

public class MainActivity extends SimpleBaseGameActivity{

	/**
	 * The main Android Activity where the game begins
	 */
	
	//=====================
	//Constants
	//=====================
	
		public static int CAMERA_WIDTH = 2048;
		public static int CAMERA_HEIGHT = 1152;
	
	//=====================
	//Fields
	//=====================	
	
		/* Graphics */
			BitmapTextureAtlas atlShip;
			ITextureRegion texShip;
			
			BitmapTextureAtlas atlPaper;
			ITextureRegion texPaper;
			
			BitmapTextureAtlas atlCtrlBase;
			ITextureRegion texCtrlBase;
			BitmapTextureAtlas atlCtrlKnob;
			ITextureRegion texCtrlKnob;
			
			BitmapTextureAtlas atlAccel;
			ITextureRegion texAccel;
			
			BitmapTextureAtlas atlBrake;
			ITextureRegion texBrake;
			
			BitmapTextureAtlas atlBullet;
			public ITextureRegion texBullet;
		
		/* Sprites */
			Sprite sprPaper;
			Ship sprShip;
			public ButtonSprite sprAccel;
			public ButtonSprite sprBrake;
			
		/* Misc */
			public Camera camera;
			public Scene scene;
			public VertexBufferObjectManager lineVBOM;

	public EngineOptions onCreateEngineOptions() {
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), camera);
		engineOptions.getTouchOptions().setNeedsMultiTouch(true);
		return engineOptions;
	}

	public void onCreateResources(){
		atlShip = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
		texShip = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlShip, this, "arrow.png", 0, 0);
		
		atlPaper = new BitmapTextureAtlas(this.getTextureManager(), 2048, 1152, TextureOptions.BILINEAR);
		texPaper = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlPaper, this, "Hi_Res_Lined_Paper_Texture_by_merileekitty.png", 0, 0);
		
		atlCtrlBase = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
		atlCtrlKnob = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		texCtrlBase = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlCtrlBase, this, "control_base.png", 0, 0);
		texCtrlKnob = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlCtrlKnob, this, "control_knob.png", 0, 0);
		
		atlAccel = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		texAccel = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlAccel, this, "accel.png", 0, 0);
		
		atlBrake = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		texBrake = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlBrake, this, "brake.png", 0, 0);
		
		atlBullet = new BitmapTextureAtlas(this.getTextureManager(), 16, 16, TextureOptions.BILINEAR);
		texBullet = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlBullet, this, "bullet.png", 0, 0);
		
		atlCtrlBase.load();
		atlCtrlKnob.load();
		atlShip.load();
		atlPaper.load();
		atlAccel.load();
		atlBrake.load();
		atlBullet.load();
	}

	public Scene onCreateScene(){
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		scene = new Scene();
		scene.setTouchAreaBindingOnActionDownEnabled(true);
		
		/* Create the background */
		sprPaper = new Sprite(0, 0, texPaper, getVertexBufferObjectManager());
		SpriteBackground bg = new SpriteBackground(sprPaper);
		scene.setBackground(bg);
		
		/* Create the ship */
		float centerX = CAMERA_WIDTH/2 - texShip.getWidth()/2;
		float centerY = CAMERA_HEIGHT/2 - texShip.getHeight()/2;
		sprShip = new Ship(centerX, centerY, texShip, getVertexBufferObjectManager(), this);
		
		/* Create the buttons */
		sprAccel = new ButtonSprite(CAMERA_WIDTH - 512 - 32, CAMERA_HEIGHT - 256 - 32, texAccel, getVertexBufferObjectManager());
		sprAccel.setScale(1.25f);
		
		sprBrake = new ButtonSprite(CAMERA_WIDTH - 256 - 32, CAMERA_HEIGHT - 512, texBrake, getVertexBufferObjectManager());
		sprBrake.setOnClickListener(new OnClickListener(){
			public void onClick(ButtonSprite pButtonSprite,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				//sprShip.shoot();
			}
			
		});
		sprBrake.setScale(1.25f);
		
		/* Create the control stick */
		final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - texCtrlBase.getHeight()-96, camera, texCtrlBase, texCtrlKnob, 0.1f, 200, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {

			public void onControlChange(BaseOnScreenControl pBaseOnScreenControl, float x, float y) {
				if (!(x == 0.0 && y == 0.0)){
					sprShip.setDirection(Util.vecToRad(new Vec(x, y)));
				}
			}

			public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {
				sprShip.resetTail();
			}
			
		});
		analogOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		analogOnScreenControl.getControlBase().setAlpha(0.5f);
		analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
		analogOnScreenControl.getControlBase().setScale(1.25f);
		analogOnScreenControl.getControlKnob().setScale(1.25f);
		analogOnScreenControl.refreshControlKnobPosition();
		
		/* And attach them all */
		scene.attachChild(sprShip);
		scene.attachChild(sprAccel);
		scene.registerTouchArea(sprAccel);
		scene.attachChild(sprBrake);
		scene.registerTouchArea(sprBrake);
		scene.setChildScene(analogOnScreenControl);
		
		return scene;
	}
	
	public void destroyEntity(IEntity i){
		if (i != null){
			final EngineLock engineLock = mEngine.getEngineLock();
			engineLock.lock();
			Log.i("destroying:", i.toString());
			scene.detachChild(i);
			i.dispose();
			i = null;
			engineLock.unlock();
		}
	}
	
	

}
